﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuanCafe.DataLayer;
using QuanCafe.BusinessLayer;

namespace QuanCafe.admin
{
    public partial class ItemEvents : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fnLoadData();
                LoadCbo();
            }
        }

        private void LoadCbo()
        {
            QuanCafe.BusinessLayer.Item Item = new QuanCafe.BusinessLayer.Item();
            cboItemID.DataSource = Item.GetAll();
            cboItemID.DataValueField = "ItemID";
            cboItemID.DataTextField = "ItemTitle";
            cboItemID.DataBind();
        }
        /// <summary>
        /// load data
        /// </summary>
        private void fnLoadData()
        {
            try
            {
                QuanCafe.BusinessLayer.ItemEvents Item = new QuanCafe.BusinessLayer.ItemEvents();
                DataTable dtb = Item.GetAll();
                dtgItem.DataSource = dtb;
                dtgItem.DataBind();

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnClearControl()
        {
            txtItemID.Text = "";
            txtFromDate.Text = "";
            txtToDate.Text = "";
            txtSetDate.Text = "";
            txtNote.Text = "";
            lblError.Text = "";
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            fnClearControl();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                QuanCafe.BusinessLayer.ItemEvents ItemObj = new QuanCafe.BusinessLayer.ItemEvents();
                if (txtItemID.Text == "")
                {
                    ItemObj.activity = "Insert";
                    ItemObj.ItemID = int.Parse(cboItemID.SelectedValue.ToString()) ;
                    ItemObj.FromDate = txtFromDate.Text.Trim();
                    ItemObj.ToDate = txtToDate.Text.Trim();
                    ItemObj.SetDate = txtSetDate.Text.Trim();
                    ItemObj.Note = txtNote.Text.Trim();
                    ItemObj.Creator = Session["AdminName"].ToString();
                    ItemObj.InsertUpdate();
                    fnLoadData();
                    lblError.Text = "Thêm mới thành công";
                    fnClearControl();
                }
                else
                {
                    ItemObj.activity = "Update";
                    ItemObj.ItemID = int.Parse(cboItemID.SelectedValue.ToString());
                    ItemObj.FromDate = txtFromDate.Text.Trim();
                    ItemObj.ToDate = txtToDate.Text.Trim();
                    ItemObj.SetDate = txtSetDate.Text.Trim();
                    ItemObj.Note = txtNote.Text.Trim();
                    ItemObj.Creator = Session["AdminName"].ToString();
                    ItemObj.InsertUpdate();
                    fnLoadData();
                    lblError.Text = "Cập nhập thành công";
                    fnClearControl();
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string strID = "";
                for (int i = 0; i < dtgItem.Items.Count; i++)
                {
                    if (((CheckBox)dtgItem.Items[i].FindControl("chkSelect")).Checked == true)
                    {
                        strID += dtgItem.Items[i].Cells[0].Text.Trim() + "$";//Cot dau tien phai de cai ID                         
                    }
                }
                CLSCommon.DeleteListRecord("SP_ItemEvents", strID, "ItemID");
                fnLoadData();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnLoadDataByID(int IntID)
        {
            try
            {
                QuanCafe.BusinessLayer.ItemEvents Item = new QuanCafe.BusinessLayer.ItemEvents();
                Item.ItemID = IntID;
                DataRow dr = Item.GetByID();
                txtItemID.Text = IntID.ToString();
                cboItemID.SelectedValue = dr["ItemID"].ToString();
                txtFromDate.Text = dr["FromDate"].ToString();
                txtToDate.Text = dr["ToDate"].ToString();
                txtSetDate.Text = dr["SetDate"].ToString();
                txtNote.Text = dr["Note"].ToString();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        protected void dtgItem_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string nName = e.CommandName.ToLower();
            int ID = -1;
            switch (nName)
            {
                case "edit":
                    ID = Convert.ToInt32(dtgItem.Items[e.Item.ItemIndex].Cells[0].Text);
                    fnLoadDataByID(ID);
                    break;
                case "activechange":
                    ID = Convert.ToInt32(dtgItem.Items[e.Item.ItemIndex].Cells[0].Text);
                    QuanCafe.BusinessLayer.ItemEvents ItemEvents = new QuanCafe.BusinessLayer.ItemEvents();
                    ItemEvents.ItemID = ID;
                    ItemEvents.ChangeActive();
                    break;
            }
            this.fnLoadData();
        }

        protected void dtgItem_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dtgItem.CurrentPageIndex = e.NewPageIndex;
            this.fnLoadData();
        }
    }
}
